package com.fly.controller;

import com.fly.domain.Department;
import com.fly.service.DepartmentService;
import com.fly.vo.Response;
import com.fly.web.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Fly
 * @Description 部门控制器
 * @Date Created in 9:09 2018/1/31
 * @Modified by
 */
@RestController
@RequestMapping("departments")
public class DepartmentController extends BaseController {

    private final DepartmentService departmentService;

    @Autowired
    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    /**
     * 获取部门列表
     *
     * @return
     */
    @GetMapping()
    public List<Department> list() {
        return departmentService.findAll();
    }

    /**
     * 获取树数据
     *
     * @param parentId
     * @return
     */
    @GetMapping("tree/{parentId}")
    public List<Department> getTree(@PathVariable("parentId") Long parentId) {
        return departmentService.getTreeData(parentId);
    }

    /**
     * 保存
     *
     * @param department
     * @return
     */
    @PostMapping()
    public ResponseEntity<Response> save(@RequestBody Department department) {
        Department rDepartment = departmentService.saveDepartment(department);
        return ResponseEntity.ok(new Response(true, "保存成功", rDepartment));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Response> remove(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return ResponseEntity.ok(new Response(true, "删除成功", null));
    }

    /**
     * 数据过滤
     *
     * @param name
     * @return
     */
    @GetMapping("filter/{name}")
    public List<Department> filterData(@PathVariable("name") String name) {
        return departmentService.findDepartmentsByName(name);
    }
}
